﻿package com.ddlive.view.utilsv;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.ddlive.R;
import com.ddlive.view.ui.myview.activity.TermsConditionActivity;

/**
 * Created : Yx
 * Time : 2018/11/28 16:30
 * Describe : TextView中某字符设置颜色和点击事件
 * Spanable中的常用常量：
 * Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含start和end所在的端点
 * Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start，但包含end所在的端点
 * Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含start，但不包含end所在的端点
 * Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含start和end所在的端点
 */
public class SpanTextUtil {

    /**
     * 条款、协议
     *
     * @param textView
     */
    public static void setClickTextViews(Context mContext, final String texts, TextView textView) {
        int startIndexs = 0;//起始位置(条款与规定)
        int endIndexs = 0;  //结束位置
        int twoStartIndex = 0;//起始位置(隐私政策)
        int twoEndIndex = 0;//结束位置
        if (texts != null && !"".equals(texts)) {
            //条款与规定
            startIndexs = texts.indexOf("Terms and Condition");
            if (startIndexs == -1) {//没找到，找中文版
                startIndexs = texts.indexOf("条款与规定");
                endIndexs = startIndexs + 5;
            } else {//找到了，英文版
                String str = "Terms and Condition";
                endIndexs = startIndexs + str.length();
            }
            //隐私政策
            twoStartIndex = texts.indexOf("Privacy Policy");
            if (twoStartIndex == -1) {//没找到，找中文版
                twoStartIndex = texts.indexOf("隐私政策");
                twoEndIndex = twoStartIndex + 4;
            } else {//找到了，英文版
                String str = "Privacy Policy";
                twoEndIndex = twoStartIndex + str.length();
            }
        }

        SpannableStringBuilder spannable = new SpannableStringBuilder(texts);
        //背景颜色

        //某个字符点击事件
        spannable.setSpan(new TextClick(mContext, R.color.color_0089CF, 1), startIndexs, endIndexs, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//SEE(前后都不包括)、SIE前包括后不包括、SEI前不包括后包括、SII前后都包括
        spannable.setSpan(new TextClick(mContext, R.color.color_0089CF, 2), twoStartIndex, twoEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//SEE(前后都不包括)、
        //这个一定要记得设置，不然点击不生效
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannable);
        textView.setOnClickListener(new View.OnClickListener() {//整项点击
            @Override
            public void onClick(View v) {
                //LogUtil.iYx("========点击了==========" + positions + "===" + CircleFragment.positions);
            }
        });
    }

    private static class TextClick extends ClickableSpan {
        private Context mContext;
        private int mColor;
        private int mIndex;//点的谁


        public TextClick(Context mContexts, int colors, int mIndexs) {
            this.mContext = mContexts;
            this.mColor = colors;
            this.mIndex = mIndexs;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(ContextCompat.getColor(mContext, mColor));
            ds.setUnderlineText(false);//设置不显示下划线

        }

        @Override
        public void onClick(View widget) {
            //((TextView) widget).setHighlightColor(ContextCompat.getColor(mContext, R.color.transparent));//点击背景色
//            textView.setHighlightColor(ContextCompat.getColor(mContext, R.color.red));
            //在此处理点击事
            if (mIndex == 1) {
                //跳到条款与规定
//                ActivityUtil.toTACorPPActivity(mContext, 99);
                Intent gotoIntent = TermsConditionActivity.getGotoIntent(mContext, 1);
                mContext.startActivity(gotoIntent);

            } else if (mIndex == 2) {
                //跳到隐私政策
//                ActivityUtil.toTACorPPActivity(mContext, 99);
                Intent gotoIntent = TermsConditionActivity.getGotoIntent(mContext, 2);
                mContext.startActivity(gotoIntent);
            }
        }


    }


}
